﻿    using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System.Data;

namespace CommonLib
{
    public class RegionOperate
    {

        #region 定义全局变量
        private AxMapControl m_axMapControl;
        //private List<string> m_cityNameList = new List<string>();
        //private List<string> m_quxianNameList = new List<string>();
        //private List<string> m_xiangzhenNameList = new List<string>();
        private XMLConfig m_readConfig = new XMLConfig("MainConfig.xml");
        private string m_cityLayerName; //市图层名
        private string m_quxianName;    //区县图层名
        private string m_xiangzhenName;//乡镇图层名
        private string m_lxdwName;//零星地物图层名
        private string m_xzdwName;//线状地物图层名
        private string m_fdysName;//分等要素图层名
        private string m_dltbName;//地类图斑图层名

        //private string m_classifyLayerName="";//分类图层名

        private DataTable m_tempTable = new DataTable();

        private IFeatureLayer m_cityFeatureLayer;     //市图层
        private IFeatureLayer m_quxianFeatureLayer;   //区县图层
        private IFeatureLayer m_xiangzhenFeatureLayer;//乡镇图层
        private IFeatureLayer m_lxdwFeatureLayer;//零星地物图层
        private IFeatureLayer m_xzdwFeatureLayer;//线状地物图层
        private IFeatureLayer m_fdysFeatureLayer;//分等要素图层
        private IFeatureLayer m_dltbFeatureLayer;//地类图斑图层
        private List<int> m_selectFeatureOidList = new List<int>();
        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        public RegionOperate(AxMapControl axMapControl)
        {
            m_axMapControl = axMapControl;

            #region //获取市、区县、乡镇的图层名和图层
            m_readConfig = new XMLConfig("MainConfig.xml");
            m_cityLayerName = m_readConfig.GetAttributeValueFromSpcialAttribute("config", "name", "市行政区划图层名", "value");
            m_quxianName = m_readConfig.GetAttributeValueFromSpcialAttribute("config", "name", "区县行政区划图层名", "value");
            m_xiangzhenName = m_readConfig.GetAttributeValueFromSpcialAttribute("config", "name", "乡镇行政区划图层名", "value");

            m_cityFeatureLayer = AETools.GetFeatureLayer(m_axMapControl, m_cityLayerName);
            m_quxianFeatureLayer = AETools.GetFeatureLayer(m_axMapControl, m_quxianName);
            m_xiangzhenFeatureLayer = AETools.GetFeatureLayer(m_axMapControl, m_xiangzhenName);
            #endregion 

            #region 获取各种地物的图层名和图层
            m_lxdwName = m_readConfig.GetAttributeValueFromSpcialAttribute("config", "name", "零星地物图层名", "value");
            m_xzdwName = m_readConfig.GetAttributeValueFromSpcialAttribute("config", "name", "线状地物图层名", "value");
            m_fdysName = m_readConfig.GetAttributeValueFromSpcialAttribute("config", "name", "分等要素图层名", "value");
            m_dltbName = m_readConfig.GetAttributeValueFromSpcialAttribute("config", "name", "地类图斑图层名", "value");

            m_lxdwFeatureLayer = AETools.GetFeatureLayer(m_axMapControl, m_lxdwName );
            m_xzdwFeatureLayer = AETools.GetFeatureLayer(m_axMapControl, m_xzdwName);
            m_fdysFeatureLayer = AETools.GetFeatureLayer(m_axMapControl, m_fdysName);
            m_dltbFeatureLayer = AETools.GetFeatureLayer(m_axMapControl, m_dltbName);
            #endregion

            #region 建立一个存储乡镇名称的表，字段为“ZMC”（镇名称）
            DataColumn column0 = new DataColumn("ZMC", Type.GetType("System.String"));
            m_tempTable.Columns.Add(column0);
            #endregion

        }

        /// <summary>
        /// 获取市图层
        /// </summary>
        /// <returns>IFeatureLayer</returns>
        public IFeatureLayer getCityFeatureLayer()
        {
            if (m_cityFeatureLayer == null) MessageBox.Show("请先加载地市图层！", "提示");
            return this.m_cityFeatureLayer;
        }

        /// <summary>
        /// 获取区县图层
        /// </summary>
        /// <returns>IFeatureLayer</returns>
        /// 
        public IFeatureLayer getQuxianFeatureLayer()
        {
            if (m_quxianFeatureLayer == null) MessageBox.Show("请先加载区县图层！", "提示");
            return this.m_quxianFeatureLayer;
        }

        /// <summary>
        ///获取乡镇图层
        /// </summary>
        /// <returns>IFeatureLayer</returns>
        public IFeatureLayer getXiangzhenFeatureLayer()
        {
            if (m_xiangzhenFeatureLayer == null) MessageBox.Show("请先加载乡镇图层！", "提示");
            return this.m_xiangzhenFeatureLayer;
        }

        /// <summary>
        /// 获取所有市名（List）
        /// </summary>
        /// <returns></returns>
        public List<string> getCityNameList()
        {
            List<string> cityNameList = new List<string>();
            if (m_cityFeatureLayer == null)
            {
                MessageBox.Show("请先加载地市图层！", "提示");
                return null;
            }
            IFeatureClass featureClass = m_cityFeatureLayer.FeatureClass;

            #region 查询
            if (featureClass == null) return null;
            m_selectFeatureOidList.Clear();
            IQueryFilter queryFilter = new QueryFilter();
            queryFilter.WhereClause = "1=1";
            IFeatureCursor featureCursor = featureClass.Search(queryFilter, true);
            IFeature feature =  featureCursor.NextFeature();
            while (feature != null)
            {
                //axMapControl.Map.SelectFeature(pLayer, feature);
                m_selectFeatureOidList.Add(feature.OID);
                cityNameList.Add(feature.get_Value(feature.Fields.FindField("CityName")).ToString());
                feature = featureCursor.NextFeature();
            }
            //m_axMapControlMain.Refresh();
            //axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            #endregion

            return cityNameList;
        }

        /// <summary>
        /// 根据指定市名，获取其所有区县名（List）
        /// </summary>
        /// <param name="cityName">市名</param>
        /// <returns></returns>
        public List<string> getQuxianNameList(string cityName)
        {
            if (m_quxianFeatureLayer == null)
            {
                MessageBox.Show("请先加载区县图层！", "提示");
                return null;
            }
            List<string> quxianNameList = new List<string>();
            IFeatureClass featureClass = m_quxianFeatureLayer.FeatureClass;

            #region 查询
            if (featureClass == null) return null;
            m_selectFeatureOidList.Clear();
            IQueryFilter queryFilter = new QueryFilter();
            queryFilter.WhereClause = ("SSCityName='" + cityName + "'");
            IFeatureCursor featureCursor = featureClass.Search(queryFilter, true);
            IFeature feature = featureCursor.NextFeature();
            while (feature != null)
            {
                m_selectFeatureOidList.Add(feature.OID);
                quxianNameList.Add(feature.get_Value(feature.Fields.FindField("QXName")).ToString());
                feature = featureCursor.NextFeature();
            }
            #endregion

            return quxianNameList;
        }

        /// <summary>
        /// 根据指定区县，获取其所有乡镇名（List）
        /// </summary>
        /// <param name="quxianName">区县名</param>
        /// <returns></returns>
        public List<string> getXiangzhenNameList(string quxianName)
        {
            if (m_xiangzhenFeatureLayer == null)
            {
                MessageBox.Show("请先加载乡镇图层！", "提示");
                return null;
            }
            List<string> zhenNameList = new List<string>();
            List<string> zhenNameList_temp = new List<string>();
            m_tempTable.Clear();
            if (quxianName != "化州市")
            {
                List<string> temp = new List<string>();
                temp.Add("乡镇级数据暂时缺省");
                return temp ;
                //xiangzhenNameList.Add("所有乡镇");
            }
            else
            {
                #region 查询
                m_selectFeatureOidList.Clear();
                IQueryFilter queryFilter = new QueryFilter();
                queryFilter.WhereClause = "1=1";
                IFeatureCursor featureCursor = m_xiangzhenFeatureLayer.FeatureClass.Search(queryFilter, true);
                IFeature feature = featureCursor.NextFeature();
                while (feature != null)
                {
                    m_selectFeatureOidList.Add(feature.OID);
                    zhenNameList_temp.Add(feature.get_Value(feature.Fields.FindField("ZMC")).ToString());
                    feature = featureCursor.NextFeature();
                }
                //DataColumn column0 = new DataColumn("ZMC", Type.GetType("System.String"));
                //m_tempTable.Columns.Add(column0);
                zhenNameList=zhenNameList_temp.Distinct().ToList();
                //for (int i = 0; i < zhenNameList_temp.Count; i++)
                //{
                //    DataRow dr = m_tempTable.NewRow();
                //    dr[0] = zhenNameList_temp[i].ToString();
                //    m_tempTable.Rows.Add(dr);

                //}
                //DataTable newTable = m_tempTable.DefaultView.ToTable(true, new string[] { "" + "ZMC" + "" });
                //for (int i = 0; i < newTable.Rows.Count; i++)
                //{
                //    zhenNameList.Add(newTable.Rows[i]["" + "ZMC" + ""].ToString());
                //}
                #endregion
            }
            return zhenNameList;
        }

        /// <summary>
        /// 根据指定区县，获取其所有村名称（List）
        /// </summary>
        /// <param name="quxianName">区县名</param>
        /// <returns></returns>
        public List<string> getCunNameList(string ZMC)
        {
            if (m_xiangzhenFeatureLayer == null)
            {
                MessageBox.Show("请先加载乡镇图层！", "提示");
                return null;
            }
            List<string> zhenNameList = new List<string>();
                #region 查询
                m_selectFeatureOidList.Clear();
                IQueryFilter queryFilter = new QueryFilter();
                queryFilter.WhereClause = ("ZMC='" + ZMC + "'");
                IFeatureCursor featureCursor = m_xiangzhenFeatureLayer.FeatureClass.Search(queryFilter, true); 
                IFeature feature = featureCursor.NextFeature();
                while (feature != null)
                {
                    m_selectFeatureOidList.Add(feature.OID);
                    zhenNameList.Add(feature.get_Value(feature.Fields.FindField("XZQHMC")).ToString());
                    feature = featureCursor.NextFeature();
                }
                #endregion
            return zhenNameList;
        }

        /// <summary>
        /// 通过地市名，获取地市的geometry
        /// </summary>
        /// <param name="cityName">地市名</param>
        /// <returns>geometry</returns>
        public IGeometry getCityGeometry(string cityName) 
        {
            if (m_cityFeatureLayer == null)
            {
                MessageBox.Show("请先加载地市图层！", "提示");
                return null;
            }
            IQueryFilter queryFilter = new QueryFilter();
            queryFilter.WhereClause = "CityName ='" + cityName + "'";
            IFeatureCursor featureCursor = m_cityFeatureLayer.FeatureClass.Search(queryFilter, true);
            IFeature feature = featureCursor.NextFeature();
            return feature.Shape;
        }

        /// <summary>
        /// 通过区县名，获取区县的geometry
        /// </summary>
        /// <param name="quxianName">区县名</param>
        /// <returns>geometry</returns>
        public IGeometry getQuxianGeometry(string quxianName)
        {
            if (m_quxianFeatureLayer == null)
            {
                MessageBox.Show("请先加载区县图层！", "提示");
                return null;
            }
            IQueryFilter queryFilter = new QueryFilter();
            queryFilter.WhereClause = "QXName='" + quxianName + "'";
            IFeatureCursor featureCursor = m_quxianFeatureLayer.FeatureClass.Search(queryFilter, true);
            IFeature feature = featureCursor.NextFeature();
            return feature.Shape;
        } 

        /// <summary>
        /// 通过乡镇名，获取乡镇的geometry
        /// </summary>
        /// <param name="xiangzhenName">乡镇名</param>
        /// <returns>geometry</returns>
        public IGeometry getXiangzhenGeometry(string xiangzhenName)
        {
            if (m_xiangzhenFeatureLayer == null)
            {
                MessageBox.Show("请先加载乡镇图层！", "提示");
                return null;
            }
            IQueryFilter queryFilter = new QueryFilter();
            queryFilter.WhereClause = "ZMC='" + xiangzhenName  + "'";
            IFeatureCursor featureCursor =m_xiangzhenFeatureLayer.FeatureClass.Search(queryFilter, true);
            IFeature feature = featureCursor.NextFeature();
            return feature.Shape;
        }

        /// <summary>
        /// 通过图层名，获取图层的所有字段名
        /// </summary>
        /// <param name="layerFieldNameList">图层字段名</param>
        /// <returns>图层字段名</returns>
        public List<string> getLayerFieldNameList(IFeatureLayer layerName)
        {
            if (m_xzdwFeatureLayer == null)
            {
                MessageBox.Show("请先加载乡镇图层！", "提示");
                return null;
            }
            List<string> layerFieldNameList = new List<string>();
            IFeatureClass featureClass = m_xzdwFeatureLayer.FeatureClass;
            if (layerName.Name != "线状地物")
            {
                List<string> temp = new List<string>();
                temp.Add("线状地物暂时缺省");
                return temp;
                //layerFieldNameList.Add("线状地物所有字段名");
            }
            else
            {
                #region 查询字段名
                //字段名称
                string strFldName;
                for (int i = 0; i < featureClass.Fields.FieldCount; i++)
                {
                    strFldName = featureClass.Fields.get_Field(i).Name;
                    //字段名称加入layerFieldNameList
                    layerFieldNameList.Add(strFldName);
                    MessageBox.Show(strFldName);//zww
                }
                //显示字段名
                #endregion
            }
            return layerFieldNameList;
        }

    }
}
